Simple Two-layer model

Based on GrFNN-Toolbox-1.0 example2.m


In [6]:
# 0. Preliminares

%matplotlib inline


import sys
sys.path.append('../')  # needed to run the examples from within the package folder

import numpy as np
import matplotlib.pyplot as plt
import mpld3
# mpld3.enable_notebook()

from pygrfnn.network import Model, make_connections
from pygrfnn.oscillator import Zparam
from pygrfnn.grfnn import GrFNN
from pygrfnn.vis import plot_connections
from pygrfnn.vis import tf_detail

In [7]:
# 1. Create Stimulus: Complex sinusoid

sr = 4000.0  # sample rate
dt = 1.0/sr
t = np.arange(0, 1, dt)
fc = 100.0  # frequency
A = 0.025  # amplitude
s = A * np.exp(1j * 2 * np.pi * fc * t)

# ramp signal linearly up/down
ramp_dur = 0.01  # in secs
ramp = np.arange(0, 1, dt / ramp_dur)
env = np.ones(s.shape, dtype=float)
env[0:len(ramp)] = ramp
env[-len(ramp):] = ramp[::-1]
# apply envelope
s = s * env

# plot stimulus
plt.plot(t, np.real(s))
plt.plot(t, np.imag(s))
plt.title('Stimulus')
mpld3.display()


Out[7]:

In [12]:
# Explore different parameter sets

params1 = Zparam(0.01, -1, -10, 0, 0, 1)  # Linear
params2 = Zparam(  -1,  4,  -3, 0, 0, 1)  # Critical


# Make the model
layer1 = GrFNN(params1, 
               frequency_range=(50,200), 
               num_oscs=200,
               stimulus_conn_type='linear')

layer2 = GrFNN(params2, 
               frequency_range=(50,200), 
               num_oscs=200)

C = make_connections(layer1,  # source layer
                     layer2,  # destination layer
                     1,  # connection strength multiplicative factor
#                      0.028712718  # std dev(eye-balled to closely match that of GrFNN =-Toolbox-1.0 example)
                     0.00015
                     )
model = Model()
model.add_layer(layer1)  # layer one will receive the external stimulus (channel 0 by default)
model.add_layer(layer2, input_channel=None)  # layer 2 is a hidden layer (no externa input)

conn = model.connect_layers(layer1, layer2, C, '1freq')


# plt.plot(np.abs(C[len(layer2.f)/2,:]))
# plt.plot(np.angle(C[len(layer2.f)/2,:]))
# mpld3.disable_notebook()
plot_connections(conn, title='Connection matrix (abs)')
mpld3.display()


Out[12]:

Run the network


In [13]:
model.run(s, t, dt)


4000/4000 done!

Plot Results


In [14]:
# First layer
tf_detail(layer1.TF, t, layer1.f, 'Layer 1', np.max(t), np.real(s), np.abs)
# mpld3.display()
plt.show()



In [15]:
# Second Layer
tf_detail(layer2.TF, t, layer2.f, 'Layer 2', np.max(t)*np.array([0.1, 0.5, 0.9]), np.real(s), np.abs)
# mpld3.display()
plt.show()



In [11]:


In [ ]: